//
//  MAPrivacyView.m
//  MateAdv
//
//  Created by 茶 on 2023/9/14.
//

#import "MAPrivacyView.h"
#import <MPITextKit/MPITextKit.h>

@interface MAPrivacyView () <MPILabelDelegate>

@end

@implementation MAPrivacyView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initInterface];
    }
    return self;
}

- (void)initInterface {
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    
    UIView *contentView = [UIView new];
    contentView.backgroundColor = [UIColor whiteColor];
    contentView.layer.cornerRadius = MARatioWidth(20);
    contentView.clipsToBounds = YES;
    [self addSubview:contentView];
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.ma_offset(40);
        make.right.ma_offset(-40);
        make.centerY.mas_equalTo(0);
        make.height.ma_offset(390);
    }];
    
    UILabel *label = [UILabel new];
    label.text = @"感谢您选择：情侣大冒险";
    label.font = MAFontScaleSemibold(18);
    [contentView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.ma_offset(28);
        make.left.ma_offset(24);
    }];
    
    NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
    paragraphStyle.firstLineHeadIndent = 15;
    paragraphStyle.tailIndent = -3;
    NSString *content = @"在使用之前，请仔细阅读《用户协议》及《隐私政策》，当您选择【同意】即表示您已充分充分阅读、理解并接受前述全部内容。我们将严格按照前述政策和规则，为您提供更好的服务。若不同意将无法使用我们的产品和服务，实在抱歉。";
    NSMutableAttributedString *atrString = [[NSMutableAttributedString alloc] initWithString:content
                                                                            attributes:@{NSFontAttributeName: MAFontScaleRegular(16),
                                                                                         NSParagraphStyleAttributeName: paragraphStyle}];
    MPITextLink *privacy = [MPITextLink linkWithValue:[NSURL URLWithString:kPrivacyURL]];
    MPITextLink *agreement = [MPITextLink linkWithValue:[NSURL URLWithString:kUserAgreementURL]];

    NSRange privacyRange = [content rangeOfString:@"《隐私政策》"];
    NSRange agreementRange = [content rangeOfString:@"《用户协议》"];
    [atrString addAttribute:MPITextLinkAttributeName value:privacy range:privacyRange];
    [atrString addAttribute:MPITextLinkAttributeName value:agreement range:agreementRange];
    [atrString addAttribute:NSForegroundColorAttributeName value:MAColor(@"#FF0071") range:privacyRange];
    [atrString addAttribute:NSForegroundColorAttributeName value:MAColor(@"#FF0071") range:agreementRange];
    [atrString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:privacyRange];
    [atrString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:agreementRange];
    
    MPILabel *contentLabel = [MPILabel new];
    contentLabel.delegate = self;
    contentLabel.attributedText = atrString;
    contentLabel.font = MAFontScaleRegular(16);
    contentLabel.textAlignment = NSTextAlignmentLeft;
    contentLabel.numberOfLines = 0;
    [contentView addSubview:contentLabel];
    [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.ma_offset(16);
        make.right.ma_offset(-16);
        make.top.ma_offset(68);
    }];
    
    
    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    confirmBtn.tag = 1;
    confirmBtn.layer.cornerRadius = MARatioWidth(24);
    [confirmBtn setBackgroundColor:MAColor(@"#FF0071")];
    [confirmBtn.titleLabel setFont:MAFontScaleMedium(16)];
    [confirmBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    [confirmBtn setTitle:@"同意并继续" forState:UIControlStateNormal];
    [confirmBtn addTarget:self action:@selector(buttonEventWithButton:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:confirmBtn];
    [confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.ma_offset(20);
        make.right.ma_offset(-20);
        make.bottom.ma_offset(-66);
        make.height.ma_offset(48);
    }];
    
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.tag = 2;
    [cancelBtn.titleLabel setFont:MAFontScaleMedium(16)];
    [cancelBtn setTitleColor:MAColor(@"#999999") forState:UIControlStateNormal];
    [cancelBtn setTitle:@"不同意" forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(buttonEventWithButton:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:cancelBtn];
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.ma_offset(20);
        make.right.ma_offset(-20);
        make.bottom.ma_offset(-16);
        make.height.ma_offset(48);
    }];
}

- (void)buttonEventWithButton:(UIButton *)button {
    if (button.tag == 1) {
        MABlock(self.agreeBlock);
        [self removeFromSuperview];
    } else if (button.tag == 2) {
        [self hideAllToasts];
        [self makeToast:@"不同意则无法享受我们的服务"];
    }
}

- (void)label:(MPILabel *)label didInteractWithLink:(MPITextLink *)link forAttributedText:(NSAttributedString *)attributedText inRange:(NSRange)characterRange interaction:(MPITextItemInteraction)interaction {
    if ([link.value isKindOfClass:NSURL.class]) {
        [UIApplication.sharedApplication openURL:(NSURL *)link.value options:@{} completionHandler:nil];
    }
}


@end
